package cn.clementine.core.utils;

import cn.clementine.core.crud.AllConfigCrud;
import jakarta.annotation.Resource;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.Executor;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

@Component
public class NginxUtils {
    @Resource
    private AllConfigCrud allConfigCrud;
    public void NginxStart(){
        String nginxPath = allConfigCrud.findByCKey("nginx-work").getCValue();
        CommandLine cmdLine = CommandLine.parse(nginxPath+"\\nginx.exe");
        cmdLine.addArgument("-c");
        String nginxConfigPath = allConfigCrud.findByCKey("nginx-config").getCValue();
        cmdLine.addArgument(nginxConfigPath);
        File workingDirectory = new File(nginxPath);
        Executor executor = new DefaultExecutor();
        executor.setWorkingDirectory(workingDirectory);
        executor.setExitValue(0);  // 设置期望的退出值（可选）
        try {
            executor.execute(cmdLine);
        } catch (ExecuteException e) {
            // 处理执行异常
            e.printStackTrace();
        } catch (IOException e) {
            // 处理I/O异常
            e.printStackTrace();
        }
    }
}
